let vm=new Vue({
    el:'.page-content',
    data:function () {
        return {
            role:{},
            uid:'',
            oid:'',
            addBtn:false,
            removeBtn:false,
            chosenUser:[],
            noChosenUser:[],
            removeUser:[],
            addUser:[],
            setting:{
                data:{//设置树的数据的配置
                    simpleData: {//简单数据格式配置  一维结构节点数组 不设置默认是多维结构数组
                        enable:true,  //开启使用简单数据格式
                        // idKey: 'id', //设置节点中的id属性key  默认
                        pIdKey: 'parentId' //设置父节点id属性key   与数据库中的字段需要对应
                    }
                },
                callback:{
                    onClick:this.onClick2
                },
                view:{//显示设置
                    //当显示某个节点/更新节点的时候，自动会调用字体设置
                    fontCss: this.fontCss1
                }
            },
            nodes:[]

        }
    },
    methods:{
        onClick2:function (event, treeId, treeNode) {
            this.selectElse(parent.layer.id,treeNode.id);
            this.search(treeNode.name)
            // $("#opneOrClose").removeClass('open')
        },
        initTree:function () {
            axios({
                url:'office/select'
            }).then(response=>{
                this.nodes=response.data.obj;
                //动态生成父节点 全部
                //初始化树(dom节点对象,配置对象,节点数组)
                let zTreeObj = $.fn.zTree.init($('#treeOffice'),this.setting,this.nodes);
                let nodes = zTreeObj.getNodes();//获取组装后的节点数组
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        search:function (sname) {
            let zTreeObj = $.fn.zTree.getZTreeObj('treeOffice');
            let nodes=zTreeObj.getNodes();
            nodes=zTreeObj.transformToArray(nodes);
            for (let i in nodes){
                nodes[i].highLight=false;
                zTreeObj.updateNode(nodes[i])
            }
            let nodesFuzzy=zTreeObj.getNodesByParamFuzzy('name',sname,null);
            for (let n in nodesFuzzy) {
                nodesFuzzy[n].highLight=true;
                zTreeObj.updateNode(nodesFuzzy[n])
            }
        },
        fontCss1:function (treeId,treeNode) {
            return treeNode.highLight?{color:'red'}:{color:'black'}
        },
        select:function (id) {
            axios({
                url:`role/selectFromRole/${id}`,
            }).then(response=>{
                this.chosenUser=response.data.obj
                for (let i in this.chosenUser) {
                    this.chosenUser[i].checked=false;
                }

            })
        },
        selectElse:function (uid,oid) {
            this.uid=uid;
            this.oid=oid;
            axios({
                url:`role/selectElse/${uid}/${oid}`,
            }).then(rs=>{
                this.noChosenUser=rs.data.obj;
                for (let i in this.noChosenUser) {
                    this.noChosenUser[i].checked=false;
                }
            })
        },
        addBtnClick:function (addId) {
            for (let i in this.noChosenUser) {
                if (this.noChosenUser[i].id === addId) {
                    this.noChosenUser[i].checked = !this.noChosenUser[i].checked;
                    if (this.noChosenUser[i].checked) {
                        this.addUser.push(addId)
                    }else {
                        for (let j in this.addUser){
                            if (this.addUser[j] === addId) {
                                this.addUser.splice(j,1);
                            }
                        }
                    }
                }
            }
            if (this.addUser.length>0){
                this.addBtn=true
            } else {
                this.addBtn=false
            }

        },
        removeBtnClick:function (removeId) {
            for (let i in this.chosenUser) {
                if (this.chosenUser[i].userId === removeId) {
                    this.chosenUser[i].checked = !this.chosenUser[i].checked;
                    if (this.chosenUser[i].checked) {
                        this.removeUser.push(removeId)
                    }else {
                        for (let j in this.removeUser){
                            if (this.removeUser[j] === removeId) {
                                this.removeUser.splice(j,1);
                            }
                        }
                    }
                }
            }
            if (this.removeUser.length>0){
                this.removeBtn=true
            } else {
                this.removeBtn=false
            }
        },
        addBtnSubmit:function () {
            // let rid=parent.layer.id;
            axios({
                url:`role/addBtnSubmit`,
                params:{"rid":parent.layer.id,"uids":this.addUser+''}
            }).then(rs=>{
                layer.msg(rs.data.msg) ;
                this.select(parent.layer.id);
                this.selectElse(this.uid,this.oid);
                this.addBtn=false;
            })
        },
        removeBtnSubmit:function () {
            axios({
                url:`role/removeBtnSubmit`,
                params:{"rid":parent.layer.id,"uids":this.removeUser+''}
            }).then(rs=>{
                layer.msg(rs.data.msg) ;
                this.select(parent.layer.id);
                this.selectElse(this.uid,this.oid);
                this.removeBtn=false;
            })
        }
    },
    created:function () {
        this.initTree();
        this.select(parent.layer.id)
    }
});